// firmware "inteligente" para el display LED 8x8 RGB de SparkFun, basado en ATmega8
// basado en el firmware original + PWM + graphic library de SAMSA
// (c) Pablo Gindel, 2010

///////// version 03, no DUMB_MODE /////////

#include "comm.h"
#include <avr/pgmspace.h>

#define sbi(var, mask)   ((var) |= (uint8_t)(1 << mask))
#define cbi(var, mask)   ((var) &= (uint8_t)~(1 << mask))

#define CLK			0
#define CLR			3
#define LATCH			2
#define DATA			1
#define EN			4
#define LISO                    0
#define DEGRADE_V               1
#define DEGRADE_H               2

#define RGB(r,g,b) (r<<4)+(g<<2)+b
#define R(c) (c>>4)
#define G(c) ((c>>2)&3)
#define B(c) (c&3)

struct COORD {byte x; byte y;};

// These arrays map the color to the actual bit
uint32_t red [] = {0x000001, 0x040000, 0x000008, 0x200000, 0x000040, 0x010000, 0x000200, 0x002000}; 
uint32_t green [] = {0x800000, 0x000004, 0x100000, 0x000020, 0x000080, 0x008000, 0x000400, 0x004000};
uint32_t blue [] = {0x000002, 0x080000, 0x000010, 0x400000, 0x020000, 0x000100, 0x001000, 0x000800};

// This holds the current picture
volatile uint8_t image_red [64];
volatile uint8_t image_green [64];
volatile uint8_t image_blue [64];

// el buffer de pantalla para los gráficos
byte buffer [8][8];

// sistema de colores de la graphic library
byte colorMap [8], colorMode;

// los caracteres, se guardan en la memoria de programa (flash)
prog_uchar caracter[96][8] PROGMEM = 
 {0, 0, 0, 0, 0, 0, 0, 0,  
  0, 0, 122, 0, 0, 122, 0, 0,  
  0, 0, 16, 96, 0, 16, 96, 0,  
  0, 36, 126, 36, 126, 36, 0, 0,  
  0, 16, 42, 127, 42, 4, 0, 0,  
  0, 98, 100, 8, 16, 38, 70, 0,  
  0, 0, 44, 82, 44, 10, 0, 0,  
  0, 0, 32, 80, 32, 0, 0, 0,  
  0, 0, 0, 62, 65, 0, 0, 0,  
  0, 0, 0, 65, 62, 0, 0, 0,  
  0, 146, 84, 56, 254, 56, 84, 146,  
  0, 16, 16, 124, 16, 16, 0, 0,  
  0, 0, 0, 1, 6, 0, 0, 0,  
  0, 0, 16, 16, 16, 16, 0, 0,  
  0, 0, 0, 6, 6, 0, 0, 0,  
  0, 0, 6, 24, 96, 0, 0, 0,  
  0, 0, 60, 66, 66, 60, 0, 0,  
  0, 0, 0, 32, 126, 0, 0, 0,  
  0, 0, 38, 74, 74, 50, 0, 0,  
  0, 0, 36, 66, 82, 44, 0, 0,  
  0, 0, 12, 52, 126, 4, 0, 0,  
  0, 0, 114, 82, 82, 76, 0, 0,  
  0, 0, 60, 82, 82, 12, 0, 0,  
  0, 0, 64, 70, 88, 96, 0, 0,  
  0, 0, 44, 82, 82, 44, 0, 0,  
  0, 0, 36, 82, 82, 60, 0, 0,  
  0, 0, 0, 54, 54, 0, 0, 0,  
  0, 0, 0, 1, 38, 0, 0, 0,  
  0, 16, 40, 68, 238, 40, 40, 56,  
  0, 0, 20, 20, 20, 20, 0, 0,  
  0, 56, 40, 40, 238, 68, 40, 16,  
  0, 0, 32, 74, 80, 32, 0, 0,  
  0, 28, 34, 73, 85, 77, 36, 24,  
  0, 62, 72, 72, 72, 62, 0, 0,  
  0, 126, 82, 82, 82, 44, 0, 0,  
  0, 60, 66, 66, 66, 36, 0, 0,  
  0, 126, 66, 66, 66, 60, 0, 0,  
  0, 0, 126, 82, 82, 66, 0, 0,  
  0, 0, 126, 80, 80, 64, 0, 0,  
  0, 60, 66, 66, 74, 46, 0, 0,  
  0, 0, 126, 16, 16, 126, 0, 0,  
  0, 0, 0, 126, 0, 0, 0, 0,  
  0, 0, 4, 2, 2, 124, 0, 0,  
  0, 0, 126, 24, 36, 66, 0, 0,  
  0, 0, 126, 2, 2, 2, 0, 0,  
  0, 126, 32, 16, 32, 126, 0, 0,  
  0, 126, 96, 24, 6, 126, 0, 0,  
  0, 60, 66, 66, 66, 60, 0, 0,  
  0, 0, 126, 72, 72, 48, 0, 0,  
  0, 60, 66, 74, 68, 58, 0, 0,  
  0, 126, 72, 72, 72, 54, 0, 0,  
  0, 36, 82, 82, 74, 36, 0, 0,  
  0, 64, 64, 126, 64, 64, 0, 0,  
  0, 124, 2, 2, 2, 124, 0, 0,  
  0, 96, 24, 6, 24, 96, 0, 0,  
  0, 96, 24, 6, 24, 6, 24, 96,  
  0, 66, 36, 24, 36, 66, 0, 0,  
  0, 64, 32, 30, 32, 64, 0, 0,  
  0, 0, 70, 74, 82, 98, 0, 0,  
  24, 12, 6, 243, 243, 6, 12, 24,  
  0, 0, 96, 24, 6, 0, 0, 0,  
  24, 48, 96, 207, 207, 96, 48, 24,  
  0, 16, 48, 95, 129, 95, 48, 16,  
  0, 84, 84, 84, 84, 84, 84, 0,  
  0, 126, 0, 126, 0, 126, 0, 0,  
  0, 0, 4, 42, 42, 30, 0, 0,  
  0, 0, 126, 18, 18, 12, 0, 0,  
  0, 0, 12, 18, 18, 18, 0, 0,  
  0, 0, 12, 18, 18, 126, 0, 0,  
  0, 0, 28, 42, 42, 26, 0, 0,  
  0, 0, 62, 80, 80, 0, 0, 0,  
  0, 0, 16, 41, 41, 62, 0, 0,  
  0, 0, 126, 16, 16, 14, 0, 0,  
  0, 0, 0, 46, 0, 0, 0, 0,  
  0, 0, 0, 1, 94, 0, 0, 0,  
  0, 0, 126, 8, 20, 2, 0, 0,  
  0, 0, 0, 126, 0, 0, 0, 0,  
  0, 30, 16, 30, 16, 14, 0, 0,  
  0, 0, 30, 16, 16, 14, 0, 0,  
  0, 0, 12, 18, 18, 12, 0, 0,  
  0, 0, 31, 20, 20, 8, 0, 0,  
  0, 0, 8, 20, 20, 31, 0, 0,  
  0, 0, 30, 16, 16, 0, 0, 0,  
  0, 0, 18, 42, 42, 36, 0, 0,  
  0, 0, 124, 34, 34, 0, 0, 0,  
  0, 0, 28, 2, 2, 28, 0, 0,  
  0, 16, 12, 2, 12, 16, 0, 0,  
  0, 24, 6, 24, 6, 24, 0, 0,  
  0, 34, 20, 8, 20, 34, 0, 0,  
  0, 0, 57, 5, 5, 62, 0, 0,  
  0, 0, 18, 22, 26, 18, 0, 0,  
  0, 0, 8, 28, 62, 127, 0, 0,  
  0, 8, 12, 250, 129, 250, 12, 8,  
  0, 0, 127, 62, 28, 8, 0, 0,  
  0, 24, 32, 32, 24, 4, 4, 24,  
  0, 30, 34, 66, 66, 34, 30, 0};

float seno [16] = {0.0, 0.19134173, 0.35355338, 0.46193975, 0.5, 0.46193975, 0.35355338, 0.19134164, -4.371139E-8, -0.19134171, -0.35355344, -0.46193984, -0.5, -0.46193972, -0.35355327, -0.19134171};
// float coseno [16] = {0.5, 0.46193975, 0.35355338, 0.19134171, -2.1855694E-8, -0.19134176, -0.35355338, -0.4619398, -0.5, -0.46193975, -0.35355332, -0.19134156, 5.9624403E-9, 0.1913418, 0.3535535, 0.46193978};

void setup () {
  ioinit ();                    // Initialize IO and SPI
  presentacion2 ();
  // cls ();                       // clear screen
  // setColor (DEGRADE_H, 4, 7);   // setea el modo de color por defecto
}

void loop () {	
      
   leer_serial ();
    
   refresh_screen ();   // actualiza la pantalla

}

